﻿using System;
using Helper;

namespace FileRenamer
{
	public class Expression
	{
		/// <summary>
		/// Gets or sets the input text to parse.
		/// </summary>
		public String Input;

		/// <summary>
		/// Gets or sets the special characters to use (escape, open, and close).
		/// Default values are: \\, {, }
		/// </summary>
		public SpecialChars Special = new SpecialChars('\\', '{', '}');

		/// <summary>
		/// Creates a new expression with nothing initialized.
		/// </summary>
		public Expression()
		{
		}

		/// <summary>
		/// Creates a new expression.
		/// </summary>
		/// <param name="input">The input text to parse.</param>
		public Expression(String input) : this()
		{
			this.Input = input;
		}

		/// <summary>
		/// Parses and evaluates the input text.
		/// </summary>
		/// <returns>The evaluated command.</returns>
		public String Parse()
		{
			return parseText(this.Input, this.Special);
		}

		/// <summary>
		/// Parses the input text.
		/// </summary>
		/// <param name="input">The text to parse.</param>
		/// <param name="special">The special characters.</param>
		/// <returns>The evaluated text.</returns>
		public static String Parse(String input, SpecialChars special)
		{
			return parseText(input, special);
		}

		/// <summary>
		/// Use the CodeBlock Helper class to parse the nested codeblocks
		/// Create a new command builder with that codeblock
		/// Upon creating the command builder the command will be evaluated and store in Output
		/// </summary>
		/// <param name="input">The command</param>
		/// <param name="special">Deliminators & escape chars</param>
		/// <returns>The evaluated command.</returns>
		private static string parseText(String input, SpecialChars special)
		{
			try
			{
				CodeBlock codeBlock = new CodeBlock(input);
				CommandBuilder builder = new CommandBuilder(codeBlock);
				string output = builder.Output;

				return output;
			}
			catch (Exception e)
			{
				//MessageBox.Show(e.Message);
				return "";
				//return "Message: " + e.Message + "\n Inner: " + e.InnerException;
			}
		}

		//public class Command
		//{
		//  public String commandRef;
		//  public Delegate action;
		//  public Command()
		//  {
		//  }
		//  public Command(String text, Delegate action)
		//  {
		//    this.commandRef = text;
		//    this.action = action;
		//  }
		//}
	}
}
